﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DAL;
using Model;

namespace Pay1
{
    public partial class test : Form
    {
        SqlDataAdapter SDA = new SqlDataAdapter();
        DataTable DT = new DataTable();
        public test()
        {
            InitializeComponent();
            TestList();
        }

        public void TestList()
        {
            SqlConnection conn = DAL.DBHelper.Connection;
            SqlCommand SCD = new SqlCommand("select * from Test", conn);
            SDA.SelectCommand = SCD;
            SDA.Fill(DT);
            dataGridView1.DataSource = DT;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
                int d = SDA.Update(DT);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
//             DAL.DBHelper.Update("select * from Test");
            MessageBox.Show("更新成功!");
        }

        public void DetailList()
        {
            this.dataGridView1.DataSource = DAL.PayDetail.GetDetailList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt = DAL.PaySpend.GetList();
            this.dataGridView1.DataSource = dt;
        }

        public DataTable List()
        {
            dataGridView1.Columns.Clear();
            DataTable dt = DAL.PaySpend.GetList();
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("tmp", typeof(string));
            for (int i = 0; i < dt.Rows.Count; i++ )
            {
                string str = dt.Rows[i]["编号"].ToString();
                DataRow dr = dt2.NewRow();
                dr[0] = NumModified(str);
                dt2.Rows.Add(dr);
            }
            this.dataGridView1.DataSource = dt2;
            return dt;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            List();
            MessageBox.Show("修改完毕。");
        }

        public string NumModified(string Num)
        {
            string str = "DH"+Num.Substring(0, Num.Length - 3);
            return str;
        }

        public void NumSpendFormat(int rIndex)
        {
            DataTable dt = DAL.PaySpend.GetList();
            Model.PaySpend ps = DAL.PaySpend.GetModel(Convert.ToInt32(dt.Rows[rIndex][0].ToString()));
            ps.编号 = NumModified(dt.Rows[rIndex]["编号"].ToString());
            DAL.PaySpend.UpdateSpend(ps);
        }
        public void NumDetailFormat(int rIndex)
        {
            DataTable dt = DAL.PayDetail.GetList("");
            Model.PayDetail ps = DAL.PayDetail.GetModel(Convert.ToInt32(dt.Rows[rIndex][0].ToString()));
            ps.编号 = NumModified(dt.Rows[rIndex]["编号"].ToString());
            DAL.PayDetail.Update(ps);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("请谨慎操作，该操作将更新数据到明细数据表。", "更新数据编号", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                DataTable dt = DAL.PaySpend.GetList();
                MessageBox.Show("现在编号格式为" + dt.Rows[0]["编号"].ToString());
                if (MessageBox.Show("确定修改为" + NumModified(dt.Rows[0]["编号"].ToString()), "确定修改", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        NumSpendFormat(i);
                    }
                }
            }
            this.dataGridView1.DataSource = DAL.PayDetail.GetList("");
        }
    }
}
